<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>JS变量提升 | Storm</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1.var123console.log(a) //undefinedvar a = 1; 输出 a 为 undefined 说明var在代码执行前就将 创建变量，并初始化为 undefined  2.let123console.log(a) //ReferenceError: a is not definedlet a = 1; 输出 ReferenceError: a is not define">
<meta name="keywords" content="Javascript">
<meta property="og:type" content="article">
<meta property="og:title" content="JS变量提升">
<meta property="og:url" content="https://storm4542.github.io/archives/bd208f70.html">
<meta property="og:site_name" content="Storm">
<meta property="og:description" content="1.var123console.log(a) //undefinedvar a = 1; 输出 a 为 undefined 说明var在代码执行前就将 创建变量，并初始化为 undefined  2.let123console.log(a) //ReferenceError: a is not definedlet a = 1; 输出 ReferenceError: a is not define">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-12-20T08:53:08.132Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JS变量提升">
<meta name="twitter:description" content="1.var123console.log(a) //undefinedvar a = 1; 输出 a 为 undefined 说明var在代码执行前就将 创建变量，并初始化为 undefined  2.let123console.log(a) //ReferenceError: a is not definedlet a = 1; 输出 ReferenceError: a is not define">
  
    <link rel="alternative" href="https://storm4542.github.io/atom.xml" title="Storm" type="application/atom+xml">
  
  
    <link rel="icon" href="https://storm4542.github.io/favicon.png">
  
  <link href="static/css/css.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="static/css/style.css">
  

</head></html>
<body>
  <div id="container">
    <div id="wrap">
      <div class="outer">
        <section id="main"><article id="post-JS变量提升" class="article article-type-post" itemscope="" itemprop="blogPost">
  
  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      JS变量提升
    </h1>
  

      </header>
    
    <div class="article-meta">
      <a href="" class="article-date">
  <time datetime="2018-12-20T08:52:09.000Z" itemprop="datePublished">2018-12-20</time>
</a>
      
    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h3 id="1-var"><a href="#1-var" class="headerlink" title="1.var"></a>1.var</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//undefined</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> a = <span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<p>输出 a 为 <code>undefined</code></p>
<p>说明var在代码执行前就将 <strong>创建变量，并初始化为 undefined</strong> </p>
<h3 id="2-let"><a href="#2-let" class="headerlink" title="2.let"></a>2.let</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//ReferenceError: a is not defined</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> a = <span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<p>输出 <code>ReferenceError: a is not defined</code>，a  未定义</p>
<p>说明let在代码执行前 <strong>创建变量，并未初始化</strong></p>
<h3 id="3-function"><a href="#3-function" class="headerlink" title="3.function"></a>3.function</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">// [Function:a]</span></span><br><span class="line">a()<span class="comment">//我是a</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">a</span>(<span class="params"></span>)</span>&#123;<span class="built_in">console</span>.log(<span class="string">'我是a'</span>)&#125;</span><br></pre></td></tr></table></figure>
<p>说明 funtuon 在代码执行前  <strong>创建、初始化并赋值</strong></p>
<h3 id="4-const"><a href="#4-const" class="headerlink" title="4.const"></a>4.const</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//ReferenceError: a is not defined</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> a = <span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<p>const 与 let 相同，但是唯一区别是 const 只有 <strong>创建和初始化并没有赋值</strong></p>

      

      
        
    </div>
  </div>
  
    
<nav id="article-nav">
  
    <a href="990bab20.html" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Cordova下载并预览功能
        
      </div>
    </a>
  
  
    <a href="39d3351.html" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">JS引用类型</div>
    </a>
  
</nav>

  
</article>


<section id="comments">
  <link rel="stylesheet" href="static/css/default.css">
  <script src="static/js/gitment.browser.js"></script>
  <script>
    var gitment = new Gitment({
      owner: 'your github account',
      repo: 'your repo',
      oauth: {
        client_id: 'your client_id',
        client_secret: 'your client_secret',
      },
    })
    gitment.render('comments')
  </script>
</section>

</section>
        <aside id="sidebar">
  <nav class="menus">
  	<ul>
  		<li><a href="index.html"><i class="icon icon-home"></i></a></li>
  		
			<li><a href="index1.html"><i class="icon icon-fenlei"></i></a></li>
  		
  		
			<li><a href="index2.html"><i class="icon icon-tag"></i></a></li>
  		
  		
  			<li><a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer"><i class="icon icon-github"></i></a></li>
  		
  	</ul>
  </nav>
  <a id="go-top" href="#"><i class="icon icon-up"></i></a>
</aside>

      </div>
      <footer id="footer">
  
	<div id="footer-info" class="inner">
	  &copy; 2019 Storm 
	  - Powered by <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Hexo</a>
	  - Theme <a href="javascript:;" target="_blank" rel="external nofollow noopener noreferrer">Jane</a>
	</div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="index.html" class="mobile-nav-link">Home</a>
  
    <a href="index1.html" class="mobile-nav-link">Archives</a>
  
    <a href="index2.html" class="mobile-nav-link">Tag</a>
  
    <a href="javascript:;" class="mobile-nav-link" rel="external nofollow noopener noreferrer" target="_blank">Github</a>
  
</nav>
    
<script>
  var disqus_shortname = 'storm';
  
  var disqus_url = 'https://storm4542.github.io/archives/bd208f70.html';
  
  (function(){
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true;
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>


<script src="static/js/jquery.min.js"></script>


  <link rel="stylesheet" href="static/css/jquery.fancybox.css">
  <script src="static/js/jquery.fancybox.pack.js"></script>


<script src="static/js/script.js"></script>

  </div>
</body>
